package com.sadhoc.router.topology;

import java.io.Serializable;

public class SequenceNumber implements Serializable {
	
	private static final long serialVersionUID = -8339266542949202045L;
	private static final int MAX = 65535;
	
	private int seqNum;

	public SequenceNumber(int seqNum) {
		this.seqNum = seqNum;
	}

	public int getSeqNum() {
		return seqNum;
	}
	
	public void increase() {
		if (seqNum < MAX) 
			seqNum++;
		else
			seqNum = 0;
	}

	public int compare(Object o) {
		
		SequenceNumber s = (SequenceNumber) o;
		int s2 = s.getSeqNum();
		
		if (seqNum == s2)
			return 0;
		
		if (seqNum > s2)
			if (seqNum - s2 <= MAX/2)
				return 1;
				
		if (seqNum < s2)
			if (s2 - seqNum > MAX/2)
				return 1;		
		
		return -1;
	}
	
	public boolean equals(Object o) {
		
		SequenceNumber s = (SequenceNumber) o;
		if (seqNum == s.getSeqNum())
			return true;
		
		return false; 
		
	}
	
	public int hashCode() {
		return (seqNum);
	}


}
